{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 撰写人：姚晓琪\n",
    "* 日期：2020年11月31日"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 总结 （Python：你知道些什么？）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1字典操作\n",
    "字典是一种key_value的数字类型，又叫做键/值对。\n",
    "* 特性，字典是无序的，但是key是唯一的，不变性的，如果重复最后的一个键/值对会替换前面的，值不需要唯一的。\n",
    "* 字典的值可以取任何数据类型，但键必须是不可变的，如：字符串、数字或数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '姚嘻嘻', 'identity': '19网新学生', 'teacher': '游鸽'}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict = {\n",
    "    \"name\":\"姚晓琪\",       # name是key，“姚晓琪”是value\n",
    "    \"identity\":\"19网新学生\",\n",
    "    \"teacher\":\"游鸽\",\n",
    "    \"name\":\"姚嘻嘻\"\n",
    "}\n",
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 访问字典里的值\n",
    "字典里的值的访问用：字典名称[key]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'姚嘻嘻'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict[\"name\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 字典的增加\n",
    "向字典添加新内容的方法是增加新的键/值对，修改或删除已有键/值对。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '姚嘻嘻', 'identity': '19网新学生', 'teacher': '游鸽', 'school': '中山大学南方学院'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict[\"school\"] = \"中山大学南方学院\"      # 增加新的键/值对\n",
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '姚嘻嘻', 'identity': '19网新学生', 'teacher': '许智超', 'school': '中山大学南方学院'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict[\"teacher\"] = \"许智超\"     # 修改已有键/值对\n",
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4 字典的删除\n",
    "能删单一的元素也能清空字典，清空只需一项操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'identity': '19网新学生', 'teacher': '许智超', 'school': '中山大学南方学院'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del dict['name']\n",
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5 字典的内置函数与方法\n",
    "* cmp(dict1,dict2):比较两个字典元素(python3中删除了该函数)\n",
    "* len(dict):计算字典元素个数，即键的总数\n",
    "* str(dict):输出字典课打印的字符串表示\n",
    "* type(variable):返回输入的变量类型，如果变量是字典就返回字典类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict1 = {\n",
    "    \"name1\":\"本特利\",\n",
    "    \"name2\":\"威廉\"\n",
    "}\n",
    "len(dict1)     # 计算字典元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"{'name1': '本特利', 'name2': '威廉'}\""
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(dict1)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.6 字典的嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'威廉': {'年龄': '5岁', '星座': '巨蟹座', '国籍': '韩国'},\n",
       " '本特利': {'年龄': '4岁', '星座': '天蝎座', '国籍': '韩国'}}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FamousDict = {\n",
    "    \"威廉\":{\n",
    "        \"年龄\":\"5岁\",\n",
    "        \"星座\":\"巨蟹座\",\n",
    "        \"国籍\":\"韩国\"\n",
    "    },\n",
    "    \"本特利\":{\n",
    "        \"年龄\":\"4岁\",\n",
    "        \"星座\":\"天蝎座\",\n",
    "        \"国籍\":\"韩国\"\n",
    "    }\n",
    "}\n",
    "FamousDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'巨蟹座'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FamousDict[\"威廉\"][\"星座\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.7 迭代处理字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个只带字母的字符串jkluiofahe\n",
      "a is found 1 time(s)\n",
      "e is found 1 time(s)\n",
      "i is found 1 time(s)\n",
      "o is found 1 time(s)\n",
      "u is found 1 time(s)\n"
     ]
    }
   ],
   "source": [
    "voles = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"请输入一个只带字母的字符串\")\n",
    "found = {}\n",
    "found[\"a\"] = 0\n",
    "found[\"e\"] = 0\n",
    "found[\"i\"] = 0\n",
    "found[\"o\"] = 0\n",
    "found[\"u\"] = 0\n",
    "for i in word:\n",
    "    if i in voles:\n",
    "        found[i] +=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,\"is found\",v,\"time(s)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.8 字典中 in和not in的用法\n",
    "可以用in操作符来检查一个字典中是否存在这个键，这样可以有效的避免字典取值时出现的keyerror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apples': 10, 'bananas': 1}\n"
     ]
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits[\"apples\"] = 10\n",
    "if \"bananas\" in fruits:\n",
    "    True\n",
    "elif \"bananas\" not in fruits:\n",
    "    fruits[\"bananas\"] = 0\n",
    "fruits[\"bananas\"] +=1\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.9 字典中的setdefault()方法\n",
    "setdefault(key,value)的表达方式\n",
    "setdefault是完成2行if/not in语句所做的工作，不过是用一行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apples': 10, 'bananas': 1}\n"
     ]
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits[\"apples\"] = 10\n",
    "fruits.setdefault(\"bananas\",0)\n",
    "fruits[\"bananas\"]+=1\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jupyter': ['操作：95', '意义：95'], 'pycharm': ['操作：85', '意义：90']}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_dict = {\n",
    "        \"jupyter\":[\"操作：95\",\"意义：95\"],\n",
    "        \"pycharm\":[\"操作：85\",\"意义：90\"],\n",
    "}\n",
    "python_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 集合\n",
    "集合不允许有重复，集合是用大括号包围着，集合中只有逗号，绝对没有冒号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 集合的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'd', 'e', 'i', 't'}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {\"a\",\"e\",\"e\",\"t\",\"i\",\"d\"}      # 相同元素会自动合并\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 集合的快速创建\n",
    "集合中用set()函数可以快速创建一个集合（例如一个字符串）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'c', 'h', 'l', 'o', 's'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = set(\"school\")\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 集合和列表的转换\n",
    "利用sorted()与list()函数将一个集合转化为一个有序的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c', 'h', 'l', 'o', 's']"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = sorted(list(a))\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4 集合的合并\n",
    "union合并集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'c', 'e', 'h', 'l', 'o', 's'}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word = \"hello\"\n",
    "b = a.union(set(word))\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.5 分辨是否共有元素\n",
    "difference()告诉你哪些不是共有的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'c', 's'}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a.difference(set(word))\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6 查询共同元素\n",
    "intersection()报告共同对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'h', 'l', 'o'}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a.intersection(set(word))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jupyter': ['操作：85', '意义：90'], 'pycharm': ['操作：80', '意义：90']}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_集合= {\n",
    "        \"jupyter\":[\"操作：85\",\"意义：90\"],\n",
    "        \"pycharm\":[\"操作：80\",\"意义：90\"],\n",
    "}\n",
    "python_集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 列表\n",
    "列表是可变的，因为可以在任何时间通过增加、删除或修改对象的类型，如果愿意，完全可以在一个列表中混合不同类型的对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 列表的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 30, 50]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [10,20,30,50]\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 列表的添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 30, 50, 1]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.append(1)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 迭代对象的添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 30, 50, 1, 'j', 'k']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.extend(\"jk\")\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.4 insert()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 10, 20, 30, 50, 1, 'j', 'k']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.insert(0,100)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.5 pop()函数\n",
    "pop()函数用于移除列表中的一个元素（默认最后一个元素），并且返回该元素的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'k'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.6 列表合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 6, 3, 11, 14, 15, 10]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,4,6,3]\n",
    "b = [11,14,15,10]\n",
    "a+=b\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.7  使用list()创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list(range(10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list([1,2,3,4,5,6])\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.8 range()创建整数列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 5, 7, 9, 11, 13]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(3,15,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.9 列表元素取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list(range(0,10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = a[1]\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.10 random.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 7, 8, 5, 9]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "a = random.sample(range(1,10),5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jupyter': ['操作：95', '意义：95'], 'pycharm': ['操作：85', '意义：90']}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_列表 = {\n",
    "        \"jupyter\":[\"操作：95\",\"意义：95\"],\n",
    "        \"pycharm\":[\"操作：85\",\"意义：90\"],\n",
    "}\n",
    "python_列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 元组\n",
    "元组是一个一旦创建就不能改变的列表，任何情况下这个元组都不能再改变。通常可以把元组想象成一个常量列表。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.1 创建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2, 4, 5, 6, 8, 9, 1)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 3,2,4,5,6,8,9,1\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 index()\n",
    "Python元组index()方法用于从元组中找出某个对象第一个匹配项的索引位置，如果这个对象不在元组中会报一个异常。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.index(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "tuple.index(x): x not in tuple",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-27-30d1b264c4f1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: tuple.index(x): x not in tuple"
     ]
    }
   ],
   "source": [
    "a.index(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.3 count()统计次数\n",
    "count()方法用于统计某个元素在元组中出现的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.count(6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.4 取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[0]\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.5 while()循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "while a<10:\n",
    "    print(a)\n",
    "    a+=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jupyter': ['操作：80', '意义：85'], 'pycharm': ['操作：78', '意义：85']}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_元组 = {\n",
    "        \"jupyter\":[\"操作：80\",\"意义：85\"],\n",
    "        \"pycharm\":[\"操作：78\",\"意义：85\"],\n",
    "}\n",
    "python_元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 函数与模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.1 函数\n",
    "使用python中的代码，以函数开始，也是以函数结束。取几行代码，为它们指定一个名字，就得到了一个函数，取一组函数把他们打包成一个文件，你就得到了一个模块。\n",
    "* 1、函数引入两个关键词：def和return\n",
    "* 2、函数可以接受参数数据\n",
    "* 3、函数包含代码，通常含有注释文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 比较两个数的大小\n",
    "def test(a,b):\n",
    "    if a>=b:\n",
    "        return b\n",
    "    else:\n",
    "        return a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2 函数的传递参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "youge\n"
     ]
    }
   ],
   "source": [
    "def greet_user(username):     #username为形参，函数完成其工作时所需要的信息\n",
    "    print(username)\n",
    "greet_user(\"youge\")           #young为实参，实参是调用函数时传递给函数的信息\n",
    "\n",
    "\n",
    "#区别：形参是虚拟的，不占空间，实参是一个变量，占用空间，数据传递是单向的，实参传给形参，不能倒过来"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3 函数的返回值\n",
    "可用return语句将值返回到调用函数的代码行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yao Xiaoqi\n"
     ]
    }
   ],
   "source": [
    "def get_name(first_name,last_name):\n",
    "    full_name = last_name+\" \"+first_name\n",
    "    return full_name.title()\n",
    "name = get_name(\"xiaoqi\",\"yao\")\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.4 导入整个模块\n",
    "语法：import模块名称\n",
    "* 模块所在的py文件就是模块名称\n",
    "* 导入模块要注意模块的py文件与主程序文件在一个目录下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.5 as的用法\n",
    "用as自定义一个名称来代替模块的名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import panda as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.6 模块中导入函数\n",
    "如果一个模块中有多个函数，但我只想使用其中的一个函数，可以直接导入该函数\n",
    "* 语法：from 模块名称 import 函数名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jupyter': ['操作：80', '意义：90'], 'pycharm': ['操作：78', '意义：90']}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_函数和模块 = {\n",
    "        \"jupyter\":[\"操作：80\",\"意义：90\"],\n",
    "        \"pycharm\":[\"操作：78\",\"意义：90\"],\n",
    "}\n",
    "python_函数和模块"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
